using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.Net;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Common;


namespace Microsoft.ConnectionEngine.Samples
{
    public partial class frmServiceProviderSample : Form
    {
        public frmServiceProviderSample()
        {
            InitializeComponent();
            
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                SystemAdapterWS.SystemAdapter systemAdapter = new SystemAdapterWS.SystemAdapter();
                SystemAdapterWS.ConnectionEngineMessage result = new SystemAdapterWS.ConnectionEngineMessage();

                if (txtOutgoingMasterMessageID.Text == "")
                     result = systemAdapter.SendConnectionEngineMessage(rtfOutgoingPayload.Text,
                                                                           Convert.ToInt32(txtOutgoingMessageTypeID.Text),
                                                                           Guid.Empty,
                                                                           txtOutgoingDestinationProviderID.Text);
                else
                {
                    string strValue = "{" + txtOutgoingMasterMessageID.Text + "}"; 
                    Guid conversationID = new Guid(strValue); 

                    result = systemAdapter.SendConnectionEngineMessage(rtfOutgoingPayload.Text,
                                                                          Convert.ToInt32(txtOutgoingMessageTypeID.Text),
                                                                          conversationID,
                                                                          txtOutgoingDestinationProviderID.Text);
                }

                SetOutGoingFields(result);

                this.Cursor = Cursors.Default;
            }
            catch (SystemAdapterException sysEx)
            {
                rtfIncomingPayload.Text = sysEx.Message;
                if (sysEx.Original != null)
                    rtfIncomingPayload.Text += "\r\n\r\n" + sysEx.Original.Message;
            }
            catch (System.Exception ex)
            {
                rtfIncomingPayload.Text = ex.Message;
                if (ex.InnerException != null)
                    rtfIncomingPayload.Text += "\r\n\r\n" + ex.InnerException.Message;
            }

        }

        private void SetOutGoingFields(SystemAdapterWS.ConnectionEngineMessage msg)
        {
            txtIncomingDestinationProviderID.Text = (msg.Header.Receiver.OrganisationID == null) ? "" : msg.Header.Receiver.OrganisationID.ToString();
            txtIncomingMasterMessageID.Text = (msg.Header.Conversation.ConversationID == null) ? "" : msg.Header.Conversation.ConversationID.ToString();
            txtIncomingMessageTypeID.Text = msg.Header.InteractionType.ToString();
            txtIncomingSourceProviderID.Text = (msg.Header.Sender.OrganisationID == null) ? "" : msg.Header.Sender.OrganisationID.ToString();
            txtIncomingStatusCode.Text = msg.Header.Status.Code.ToString();
            txtIncomingStatusDescription.Text = msg.Header.Status.Description;
            rtfIncomingPayload.Text = msg.Body.Payload.Content;
        }

    }
}
